package com.hk.yajin.utills;

import android.content.Context;
import android.support.v7.widget.RecyclerView;


/*
* This class is a ScrollListener for RecyclerView that allows to show/hide
* views when list is scrolled. It assumes that you have added a header
* to your list. @see pl.michalz.hideonscrollexample.adapter.partone.RecyclerAdapter
* */
public abstract class SwichBarScrollListener extends RecyclerView.OnScrollListener {

    //threshold
    private int totalSwichHight = 200;

    private Context context;
    private int totalDy = 0;


    public SwichBarScrollListener() {

    }

    public SwichBarScrollListener(Context ct) {
        context = ct;
        totalSwichHight = AppUtils.dip2px(ct, 120);

    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        totalDy += dy;
        if (totalSwichHight > totalDy) {
            float present = (float) totalDy / (float) totalSwichHight;
            int state = (int) ((present) * 255);
            onPartSwich(state);
            //  LogUtils.d("透明度的百分比：" + state);
        } else {
            onTotalSwich();

        }


    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState); //0松手静止  //2 松手滚动  //1按住状态

        if (newState == 0 &&totalDy!=0&& totalDy < totalSwichHight)
            if (totalDy >= (totalSwichHight / 2)) {
               // recyclerView.smoothScrollToPosition(1);     //无效
               // LogUtils.d("满足条件—影藏头部");
               // recyclerView.smoothScrollBy(0, totalSwichHight / 2); //效果不稳定
                //recyclerView.smoothScrollToPosition(0);//有效
                //recyclerView.getLayoutManager().scrollToPosition(1);//无效
//                LinearLayoutManager lm= (LinearLayoutManager)recyclerView.getLayoutManager();
//                lm.scrollToPosition(1);
                int top = recyclerView.getChildAt(1).getTop();
                recyclerView.smoothScrollBy(0,top);
            } else {
                recyclerView.smoothScrollToPosition(0);//有效
            }
    }

    public abstract void onTotalSwich();

    public abstract void onPartSwich(int present);
}
